<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'
import { mapGetters } from 'vuex'
import { political } from '@/api/anniversary/statistics'

const animationDuration = 3000

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      indicatorData: [],
      politicalData: [],
      top: 5,
      chart: null
    }
  },
  computed: {
    ...mapGetters([
      'user'
    ])
  },
  mounted() {
    setTimeout(() => {
      var params = {}
      for (var i = 0; i < this.user.roles.length; i++) {
        if (this.user.roles[0].name === '学院管理员') {
          params = { college: this.user.dept.name, top: this.top }
        } else {
          params = { college: '', top: this.top }
        }
      }
      political(params).then(res => {
        if (res != null) {
          for (var i = 0; i < res.length; i++) {
            this.indicatorData.push({ name: res[i].name, max: res[i].count * (1 + Math.floor(Math.random() * 50 + 5) / 100) })
            this.politicalData.push(res[i].count)
          }

          this.initChart()
          this.__resizeHandler = debounce(() => {
            if (this.chart) {
              this.chart.resize()
            }
          }, 100)
          window.addEventListener('resize', this.__resizeHandler)
        }
      }
      )
    }, 100)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption({
        title: {
          text: '校友政治面貌分布Top ' + this.top,
          left: 'center'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        radar: {
          radius: '66%',
          center: ['55%', '55%'],
          splitNumber: 5,
          indicator: this.indicatorData
        },
        series: [{
          type: 'radar',
          symbolSize: 5,
          areaStyle: {
            normal: {
              shadowBlur: 13,
              shadowColor: 'rgba(255, 255, 255, 0.5)',
              shadowOffsetX: 0,
              shadowOffsetY: 10,
              opacity: 0.2
            }
          },
          data: [
            {
              value: this.politicalData, // [2471, 1926, 1880, 92, 15]
              name: '政治面貌',
              label: {
                show: true,
                formatter: function(params) {
                  return params.value
                }
              }
            }
          ],
          animationDuration: animationDuration
        }]
      })
    }
  }
}
</script>
